package cn.puge.mertisfront.controller;

import cn.puge.commonutils.R;
import cn.puge.mertisfront.entity.PugeUser;
import cn.puge.mertisfront.entity.PugeUserLogin;
import cn.puge.mertisfront.entity.QqOpenid;
import cn.puge.mertisfront.service.PugeUserLoginService;
import cn.puge.mertisfront.service.PugeUserService;
import cn.puge.servicebase.exceptionhandler.PuGeiException;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

/**
 * @author LIJW
 * @date 2020/11/30 17:40
 */
@RestController
@CrossOrigin
@RequestMapping("/mertisfront/LoginQqUser")
public class pugeqqLoginUser {

    @Autowired
    private PugeUserService pugeUserService;

    @Autowired
    private PugeUserLoginService pugeUserLoginService;

    /**
     * 绑定用户信息
     */
    @PostMapping("PugeUserQqOpenid")
    public R PugeUserQqOpenid(@RequestBody QqOpenid qqOpenid){

        if (StringUtils.isEmpty(qqOpenid.getOpenid())){
            throw new PuGeiException(50014,"非法操作");
        }
        if (StringUtils.isEmpty(qqOpenid.getUsername()) || StringUtils.isEmpty(qqOpenid.getPassword()) ){
            throw new PuGeiException(20001,"账户或密码不能为空");
        }
        QueryWrapper<PugeUserLogin> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username",qqOpenid.getUsername());
        PugeUserLogin one = pugeUserLoginService.getOne(queryWrapper);
        if (StringUtils.isEmpty(one)){
            throw new PuGeiException(20001,"你不是普歌的同学哦");
        }
        if (!qqOpenid.getPassword().equals(one.getPassword())){
            throw new PuGeiException(20001,"密码错误");
        }

        PugeUser pugeUser = new PugeUser();
        pugeUser.setQqOpenid(qqOpenid.getOpenid());
        pugeUser.setId(one.getId());
        pugeUserService.updateById(pugeUser);
        return R.ok().data("token",pugeUser.getId());
    }
}
